package ch.ffhs.activities;

import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.view.MenuItem;

/**
 * class SettingsActivity. Implements the settings menu. 
 * @author mf
 */
public class SettingsActivity extends Activity {
	
	/**
	 * request-code for startActivityForResult() 
	 */
	public static final int SETTINGS_REQUEST = 2;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getActionBar().setDisplayHomeAsUpEnabled(true);

		// Display the fragment as the main content.
		getFragmentManager().beginTransaction()
				.replace(android.R.id.content, new SettingsFragment()).commit();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    // Respond to the action bar's Up/Home button
	    case android.R.id.home:
	    	// set result
			this.setResult(RESULT_OK);
			// closing activity
			finish();
	        return true;
	    }
	    return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		// set result
		this.setResult(RESULT_OK);
		// closing activity
		this.finishActivity(SETTINGS_REQUEST);

		super.onBackPressed();
	}

	public static class SettingsFragment extends PreferenceFragment {

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			addPreferencesFromResource(R.xml.activity_settings);
		}
	}
}
